package com.ljh.lottery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljh.lottery.common.errorcode.ServiceErrorCodeConstants;
import com.ljh.lottery.common.exception.ServiceException;
import com.ljh.lottery.common.utils.BeanTransUtil;
import com.ljh.lottery.mapper.PrizeMapper;
import com.ljh.lottery.pojo.dataobject.PrizeDO;
import com.ljh.lottery.pojo.request.CreatePrizeRequest;
import com.ljh.lottery.pojo.request.PageRequest;
import com.ljh.lottery.service.PictureService;
import com.ljh.lottery.service.PrizeService;
import com.ljh.lottery.service.dto.PrizeDTO;
import com.ljh.lottery.service.dto.PageListDTO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author: Themberfue
 * @date: 2025/4/11 12:13
 * @description:
 */
@Service
public class PrizeServiceImpl implements PrizeService {
    @Resource(name = "pictureServiceImpl")
    PictureService pictureService;

    @Autowired
    PrizeMapper prizeMapper;

    @Override
    public Long createPrize(CreatePrizeRequest createPrizeRequest, MultipartFile multipartFile) {
        // TODO 1. 存入图片到服务器
        String fileName = pictureService.uploadPic(multipartFile);

        try {
            // TODO 2. 存入奖品数据到服务器
            PrizeDO prizeDO = BeanTransUtil.transToPrizeDO(createPrizeRequest, fileName);
            prizeMapper.insert(prizeDO);
            return prizeDO.getId();
        } catch (Exception e) {
            throw new ServiceException(ServiceErrorCodeConstants.PRIZE_CREATE_ERROR);
        }
    }

    @Override
    public PageListDTO<PrizeDTO> getPrizePageList(PageRequest param) {
        // TODO 1. 获取奖品总数
        QueryWrapper<PrizeDO> queryWrapper = new QueryWrapper<>();
        Integer total = prizeMapper.selectCount(queryWrapper).intValue();

        // TODO 2. 根据偏移量和页数获取奖品列表
        Page<PrizeDO> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        queryWrapper.lambda()
                .orderByDesc(PrizeDO::getId);

        Page<PrizeDO> prizeDOPage = prizeMapper.selectPage(page, queryWrapper);

        List<PrizeDTO> prizeDTOList = prizeDOPage.getRecords()
                .stream()
                .map(BeanTransUtil::transToPrizeDTO)
                .toList();

        return new PageListDTO<>(total, prizeDTOList);
    }

    @Override
    public PageListDTO<PrizeDTO> getAllPrize() {
        QueryWrapper<PrizeDO> queryWrapper = new QueryWrapper<>();

        // TODO 1. 获取奖品总数
        Integer total = prizeMapper.selectCount(queryWrapper)
                .intValue();

        // TODO 2. 获取所有奖品
        List<PrizeDTO> prizeDTOList = prizeMapper.selectList(queryWrapper)
                .stream()
                .map(BeanTransUtil::transToPrizeDTO)
                .toList();

        return new PageListDTO<>(total, prizeDTOList);
    }
}
